package libraries;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class LibraryContainer {

	
	LinkedList<TransPartLibrary> libraries;
	
	public LibraryContainer() {
		this.libraries = new LinkedList<TransPartLibrary>();
	}
	
	public static LibraryContainer testLibraryContainer() {
		LibraryContainer libCon = new LibraryContainer();
		libCon.addLibrary(TransPartLibrary.testLibrary());
		
		return libCon;
	}
	
	public String addLibrary (TransPartLibrary lib) {
		String check = checkLibraryConsistency(lib);
		
		if (check.equals("")) {
			libraries.add(lib);
			return "";
		} else {
			return check;
		}
	}
	
	public String checkLibraryConsistency (TransPartLibrary lib) {
		for (int i = 0; i< libraries.size(); i++) {
			if (lib.name == libraries.get(i).name) {
				return "A library with name "+ lib.name + " already exists";
			}
		}
		for (int i = 0; i < lib.size(); i++) {
			String error = checkPartConsistency(lib.get(i));
			if (error.equals("") == false){
				return error;
			}
		}
		return "";
	}
	
	public String checkPartConsistency (TransPart part) {
		for (int j = 0; j < libraries.size(); j++) {
			for (int k = 0; k < libraries.get(j).size(); j++){
				if (part.badMatches(libraries.get(k).get(j))) {
					String error = "Part " + part.toString() + " shares matching with " + libraries.get(k).get(j).toString() + " from Library " + libraries.get(k).name + ".";
					return error;
				}
			}
		}
		return "";
	}
	
	public TransPart find(String dev, String pac, String lib) {
		for (TransPartLibrary l: libraries) {
			TransPart result = l.find(dev, pac, lib);
			if (result != null) {
				return result;
			}
		}
		return null;
	}
	
	
	public void readLibrary(File libFile) throws IOException {
		BufferedReader in = null;
		LinkedList<String> text = new LinkedList<String>();
		
		try {
			in = new BufferedReader(new FileReader(libFile));
			String s;
            while ((s = in.readLine()) != null) {
            	text.add(s);
            }
            interpretLibFile(text);
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	private void interpretLibFile(LinkedList<String> text) {
		String name = text.getFirst();
		TransPartLibrary lib = new TransPartLibrary(name);
		//possibly use a higher starting index if overhead is added to library file!
		for (int i = 1; i < (text.size()-1); i++) {
			//attention: i is incremented within the loop for a second line read!
			String partS = text.get(i);
			String[] partA = partS.split("\\|");
			i++;
			String connS = text.get(i);
			String[] connA = connS.split("\\|");
			LinkedList<String> connKeys = new LinkedList<String>();
			LinkedList<String> connVals = new LinkedList<String>();
			for (String j: connA) {
				String[] keyval = j.split(" ");
				connKeys.add(keyval[0]);
				connVals.add(keyval[1]);
			}
			TransConn conn = new TransConn(connKeys, connVals);
			lib.add(new TransPart(partA[0], partA[1], partA[2], partA[3], conn, partA[4], Integer.parseInt(partA[5]), partA[6]));
		}
		addLibrary(lib);
	}
	
	public void writeLibrary(File file, String name) throws IOException {
		TransPartLibrary l = getLibrary(name);
		if (l != null) {
			l.writeLibraryFile(file);
		}
	}
	
	public TransPartLibrary getLibrary(String name) {
		for (TransPartLibrary l: libraries) {
			if (l.name == name) {
				return l;
			}
		}
		return null;
	}
}
